package com.itzxx.huazhun.data.model

import com.itzxx.huazhun.data.dao.ProbeRecordEntity
import com.itzxx.library_base_ktx.ext.util.loge

/**
 * 探头全部数据，包括采集
 */

data class ProbeAllInfo(
    val mac: String, //mac地址
    val sw: Int, //软件版本
    val hw: Int,//硬件版本
    val battery: Int,//电池
    val feature: Int,//功能
    val wayNum: Int,//通道数
    val wayIndex: String,//通道列表
    val recordSamplingInterval: String,//采集间隔
    val recordStartTime: String,//开始时间
    val model: String,//厂家
    val recordNum: Int,//记录数量
) {
    private var recordStartIndex = 1//采集下标

    private var recordStartIndexNum = 0//最初采集下标，没变动的

    var probeSamplingRecordInfoList = mutableListOf<ProbeRecordEntity>()

    private var errorNum = 0//异常数量

    private var readUpdateTime = 0L//更新时间

    //读取每笔数量
    fun getSingleRecordCallBackSize(): Int {
        return if (recordStartIndexNum + (74 / (wayNum * 2)) >= recordNum) {
            "getSingleRecordCallBackSize=${recordNum - recordStartIndexNum}".loge()
            //判断是否超数量读取
            recordNum - recordStartIndexNum
        } else {
            " 74 / (wayNum * 2)=${  74 / (wayNum * 2)}".loge()
            74 / (wayNum * 2)
        }
    }

    fun getRecordStartIndexNum(): Int {
        return recordStartIndex + recordStartIndexNum
    }

    fun plusRecordStartIndex() {
        recordStartIndexNum = recordStartIndexNum.plus(getSingleRecordCallBackSize())
    }

    //如果获取每次获取的总数量大于读取返回数量说明已经完了，正常相等
    fun isEndRead(): Boolean {
        "recordNum=$recordNum".loge()
        "recordStartIndexNum=${recordStartIndexNum}".loge()
        return recordNum <= recordStartIndexNum
    }

    //刷新最新时间
    fun refreshReadTime() {
        readUpdateTime = System.currentTimeMillis()
    }

    //默认2秒
    fun isReadTimeout(): Boolean {
        val isTimeOut = (System.currentTimeMillis() - readUpdateTime) >= 3 * 1000
        if (isTimeOut) {
            errorNum++
        }
        return isTimeOut
    }


    /**
     * 异常次数统计
     */
    fun errorOutLimit(): Boolean {
        return recordNum <= errorNum
    }

    fun initReadIndex(index: Int) {
        this.recordStartIndex = index
    }
}
